<template>
  <div class="container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-card class="form_card">
          <h1>Hello</h1>
          <el-form :model="loginForm" :rules="rules" ref="loginForms">
            <el-form-item prop="username">
              <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input :prefix-icon="Lock" type="password" show-password v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item prop="verifyCode">
              <el-input :prefix-icon="Warning" v-model="loginForm.verifyCode" placeholder="VerifyCode" size="large"
                maxlength="4" >
                <template #append>
                  <Identify :identifyCode="identifyCode" @click="refreshCode" />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button :loading="loading" class="login_btn" type="primary" size="default" @click="login">
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import { User, Lock, Warning } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import useUserStore from '@/store/modules/user'
// import { LoginFormData } from '@/api/user/type'
import { ElNotification } from 'element-plus'
import Identify from '@/components/VerifyCode/index.vue'
import { getTime } from '@/utils/time'
let $router = useRouter()
let $route = useRoute()
let useStore = useUserStore()
let loginForm = reactive({
  username: 'luofan',
  password: '123456',
  verifyCode: '1234',
})

const identifyCode = ref('1234')
const identifyCodes = ref('1234567890abcdefjhijklinopqrsduvwxyz')

// 重置验证码
const refreshCode = () => {
  identifyCode.value = ''
  makeCode(4)
}

const makeCode = (l: number) => {
  for (let i = 0; i < l; i++) {
    identifyCode.value +=
      identifyCodes.value[randomNum(0, identifyCodes.value.length)]
  }
}

const randomNum = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min) + min)
}
const validatorUsername = (_: any, value: string, callback: any) => {
  if (value.length === 0) {
    callback(new Error('请输入账号'))
  } else {
    callback()
  }
}

const validatorPassword = (_: any, value: string, callback: any) => {
  if (value.length === 0) {
    callback(new Error('请输入密码'))
  } else if (value.length < 6 || value.length > 16) {
    callback(new Error('密码应为6~16位的任意组合'))
  } else {
    callback()
  }
}

const validatorVerifyCode = (_: any, value: string, callback: any) => {
  if (value.length === 0) {
    callback(new Error('请输入验证码'))
  } else if (value.length < 4) {
    callback(new Error('请输入正确的验证码'))
  } else if (identifyCode.value !== value) {
    callback(new Error('请输入正确的验证码'))
  } else if (identifyCode.value === value) {
    callback()
  }
}

const rules = {
  username: [
    {
      trigger: 'change',
      validator: validatorUsername,
    },
  ],
  password: [
    {
      trigger: 'change',
      validator: validatorPassword,
    },
  ],
  verifyCode: [
    {
      trigger: 'blur',
      validator: validatorVerifyCode,
    },
  ],
}

let loading = ref(false)
let loginForms = ref()
const login = async () => {
  await loginForms.value.validate()
  loading.value = true
  const res = await useStore.userLogin(loginForm)
  if (res) {
    // 假设$route和$router是在组件中正确注入的Vue Router实例
    const redirect = $route.query.redirect;
    if (typeof redirect === 'string') {
      $router.push(redirect);
    } else {
      $router.push('/'); // 或者根据你的需求提供一个默认的重定向路径
    }
    ElNotification({
      type: 'success',
      title: `Hi,${getTime()}好`,
      message: '登录成功',
    })
    loading.value = false
  } else {
    ElNotification({
      type: 'error',
      message: '登录失败',
    })
    loading.value = false
  }
}
</script>
<style scoped lang="scss">
.container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  position: fixed;

  .form_card {
    position: relative;
    width: 55%;
    top: 25vh;
    left: 10vw;
    padding: 10px;
    background: transparent;

    h1 {
      background: linear-gradient(to right, blue, rgb(35, 60, 70));
      background-clip: text;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      font-size: 40px;
      text-align: center;
      font-weight: 700;
      margin-bottom: 40px;
      margin-top: -10px;
    }

    .login_btn {
      width: 100%;
    }
  }

  .el-card {
    box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
  }

  :deep(.el-input-group__append, .el-input-group__prepend) {
    padding: 0;
  }
}
</style>
